package com.zjhn.ds.common.exception;

import com.zjhn.ds.common.BizResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @ClassName CustomGlobalExceptionHandle
 * @Description 自定义异常
 * @Author sxt
 * @Date 2022/8/19 10:48
 * @Version 1.0
 */
@ControllerAdvice
@Slf4j
public class CustomGlobalExceptionHandle {

    /**
     * 自定义异常处理
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value = BizException.class)
    @ResponseBody
    public BizResult<?> handleBizException(HttpServletRequest req, BizException e){
        log.error("====[ERROR]==== CustomGlobalExceptionHandle ==== handleBizException 自定义异常, path: {} ",req.getPathInfo());
        log.error("====[ERROR]==== CustomGlobalExceptionHandle ==== handleBizException 异常： {}", e.getMessage());
        return BizResult.fail(e.getMessage());
    }

    @ExceptionHandler(value = BindException.class)
    @ResponseBody
    public BizResult<?> exceptionHandle(BindException exception) {

        BindingResult result = exception.getBindingResult();
        StringBuilder errorMsg = new StringBuilder();

        List<FieldError> fieldErrors = result.getFieldErrors();
        fieldErrors.forEach(error -> {
            log.error("field: " + error.getField() + ", msg:" + error.getDefaultMessage());
            errorMsg.append(error.getDefaultMessage()).append("!");
        });
        return BizResult.fail(errorMsg.toString());
    }

    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    @ResponseBody
    public BizResult<?> MyExceptionHandle(MethodArgumentNotValidException exception) {
        BindingResult result = exception.getBindingResult();
        StringBuilder errorMsg = new StringBuilder();
        List<FieldError> fieldErrors = result.getFieldErrors();
        fieldErrors.forEach(error -> {
            log.error("field: " + error.getField() + ", msg:" + error.getDefaultMessage());
            errorMsg.append(error.getDefaultMessage()).append("!");
        });
        return BizResult.fail(errorMsg.toString());
    }

    /**
     * 处理运行时异常
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value = RuntimeException.class)
    @ResponseBody
    public BizResult<?> handleException(HttpServletRequest req, RuntimeException e){
        log.error("====[ERROR]==== CustomGlobalExceptionHandle ==== handleException 系统错误, path: {} ",req.getPathInfo());
        log.error("====[ERROR]==== CustomGlobalExceptionHandle ==== handleException 异常： {}", e.getMessage());
        return BizResult.fail("系统错误");
    }
}
